<?php
namespace App\Models;

/**
 * 日志类
 */
class MConfig extends BaseModel
{
    public $table = "configs";

    /**
     * 按标题读取配置信息
     */
    public static function getKV($title)
    {
        return MConfig::where("title", $title)->first();;
    }

    /**
     * 存储配置信息，只有键前缀为kv_开头的数据才会被存储
     */
    public static function saveKV($title, $kv)
    {
        $config = MConfig::where("title", $title)->first();
        if (empty($config)) {
            $config = new MConfig();
            $config->title = $title;
        }

        $value = array();
        foreach ($kv as $k => $v) {
            if (starts_with($k, "kv_")) {
                $value[$k] = $v;
            }
        }
        $config->kv = serialize($value);
        $config->time = time();
        return $config->save();
    }

    /**
     * 获取全部键
     * @return 键值数组[]
     */
    public function getKeys()
    {
        $result = array();
        if (!empty($this->kv)) {
            $kv = unserialize($this->kv);
            $result = array_keys($kv);
        }
        return $result;
    }

    /**
     * 获取值
     */
    public function getVal($key)
    {
        $result = "";
        if (!empty($this->kv)) {
            $kv = unserialize($this->kv);
            $result = $kv[$key];
        }
        return $result;
    }

    /**
     * 站点信息存储验证
     * $flag 0 = rules  1 = msg
     */
    public static function webRulesWithMsg($flag)
    {
        $rules = array(
            "kv_site_name"  => "required|max:100",
            'kv_site_open'  => 'required|numeric',
            'kv_site_intro' => 'required',
        );

        $msg = array(
            "kv_site_name.required"  => "请填写站点名称！",
            "kv_site_name.max"       => "站点名称最长为100个字符！",
            "kv_site_open.required"  => "站点状态必须选择！",
            "kv_site_intro.required" => "站点描述必须填写！"
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    /**
     * 网站信息存储验证
     * $flag 0 = rules  1 = msg
     */
    public static function basicRulesWithMsg($flag)
    {
        $rules = array(
            "kv_basic_name"  => "required|max:100",
            'kv_basic_addr'  => 'required|max:100',
            'kv_basic_telephone' => 'required|max:20',
            'kv_basic_email' => 'required|max:50'
        );

        $msg = array(
            "kv_basic_name.required"  => "请填写公司名称！",
            "kv_basic_name.max"       => "公司名称最长为100个字符！",
            "kv_basic_addr.required"  => "请填写公司地址！",
            "kv_basic_addr.max"       => "公司地址最长为100个字符！",
            "kv_basic_telephone.required"  => "请填写联系电话！",
            "kv_basic_telephone.max"       => "联系电话最长为20个字符！",
            "kv_basic_email.required"  => "请填写公司邮箱！",
            "kv_basic_email.max"       => "公司邮箱最长为50个字符！"
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }
}